package cn.com.bdqn.controller;

import cn.com.bdqn.entity.SalPlan;
import cn.com.bdqn.entity.SysRoleRight;
import cn.com.bdqn.service.SysRoleRightService;
import cn.com.bdqn.utils.ReturnData;
import io.swagger.annotations.Api;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/sysRoleRight")
@Api(tags = "权限分配管理接口")
public class SysRoleRightController {

    @Autowired
    private SysRoleRightService sysRoleRightService;

    @Operation(summary = "分配权限")
    @PostMapping("/assignment")
    public ReturnData assignment(SysRoleRight sysRoleRight){

        // 调用sysRoleRightService的delSysRoleRight方法，删除对应rfRoleId的角色权限
        sysRoleRightService.delSysRoleRight(sysRoleRight.getRfRoleId());

        // 获取sysRoleRight对象的rfRightCodes数组
        String[] rfRightCodes = sysRoleRight.getRfRightCodes();

        // 创建一个新的SysRoleRight对象
        SysRoleRight sysRoleRightNew = new SysRoleRight();

        // 设置新对象的rfRoleId为原对象的rfRoleId
        sysRoleRightNew.setRfRoleId(sysRoleRight.getRfRoleId());

        // 遍历rfRightCodes数组
        for (String rfRightCode : rfRightCodes) {
            // 设置新对象的rfRightCode为Long型的rfRightCode
            sysRoleRightNew.setRfRightCode(Long.valueOf(rfRightCode));
            // 调用sysRoleRightService的addSysRoleRight方法，新增对应rfRoleId和rfRightCode的角色权限
            sysRoleRightService.addSysRoleRight(sysRoleRightNew);
        }

        // 返回成功的ReturnData对象
        return ReturnData.ok();
    }

    @Operation(summary = "根据角色编号查询应有权限")
    @PostMapping("/queryByRfRoleId")
    public ReturnData queryByRfRoleId(Long rfRoleId){
        try {
            List<SysRoleRight> sysRoleRights = sysRoleRightService.queryByRfRoleId(rfRoleId);
            return ReturnData.ok().put("sysRoleRights",sysRoleRights);
        }catch (Exception e){
            e.printStackTrace();
            return ReturnData.currError("数据库异常");
        }
    }

}
